/**
 * Created by Administrator on 2017/4/21 0021.
 */
SLine=function(getX,getY,ToX,ToY,getWidth,getHeight,getRaduis){
    var greenColor=new THREE.Color(127/255,1,1);
    var whole=new THREE.Object3D();
    var count,valueX,valueY;
    var iX=1;
    var iY=1;

    var minusX=getX-ToX;
    var minusY=getY-ToY;
    var speed=4;
    var angle=  Math.atan2(-minusY,-minusX);
    var XYDirection="X";
    var bl_DoIt=false;
    var arrPosition=[];
    if(minusX>0){
        iX=-1;
    }
    if(minusY>0){
        iY=-1;
    }
    if(Math.abs(minusX)>Math.abs(minusY)){

        XYDirection="X";
        count=minusX/((getWidth+getWidth/4)*Math.cos(angle));
        valueX=speed*iX;
        valueY=speed*Math.abs(minusY)/Math.abs(minusX)*iY;
    }
    else{
        XYDirection="Y";
        count=minusY/((getWidth+getWidth/4)*Math.sin(angle));
        valueX=speed*Math.abs(minusX)/Math.abs(minusY)*iX;
        valueY=speed*iY;
    }

    // console.log(angle);
    // console.log(count);
    // console.log(iX);

    // for(var y=0;y<(Math.abs(ToX-getX)/getWidth)+1;y++) {
    count=Math.abs(count);
    for(var y=0;y<count;y++) {
        var g = new THREE.PlaneGeometry(getWidth, getHeight, 1, 1);
        var m = new THREE.MeshBasicMaterial({color: 0x7fffff, transparent: true, side: THREE.DoubleSide});
        var linePart1 = new THREE.Mesh(g, m);
        // scene.add(linePart1);
        linePart1.position.set(0, getRaduis, 0);
        linePart1.rotation.x = Math.PI / 180 * 90;
        linePart1.rotation.y = Math.PI / 180 * -10;

        g = new THREE.PlaneGeometry(getWidth, getHeight, 1, 1);
        m = new THREE.MeshBasicMaterial({color: 0x7fffff, transparent: true, side: THREE.DoubleSide});
        var linePart2 = new THREE.Mesh(g, m);
        // scene.add(linePart2);
        linePart2.position.set(0, -getRaduis, 0);
        linePart2.rotation.x = Math.PI / 180 * 90;
        linePart2.rotation.y = Math.PI / 180 * 10;

        g = new THREE.PlaneGeometry(getWidth, getHeight, 1, 1);
        m = new THREE.MeshBasicMaterial({color: 0x7fffff, transparent: true, side: THREE.DoubleSide});
        var linePart3 = new THREE.Mesh(g, m);
        // scene.add(linePart3);
        linePart3.position.set(0, 0, getRaduis);
        linePart3.rotation.y = Math.PI / 180 * 10;
        // linePart3.rotation.z = Math.PI / 180 * 90;

        g = new THREE.PlaneGeometry(getWidth, getHeight, 1, 1);
        m = new THREE.MeshBasicMaterial({color: 0x7fffff, transparent: true, side: THREE.DoubleSide});
        var linePart4 = new THREE.Mesh(g, m);
        // scene.add(linePart4);
        linePart4.position.set(0, 0, -getRaduis);
        linePart4.rotation.y = Math.PI / 180 * -10;
        // linePart4.rotation.z = Math.PI / 180 * 90;

        linePart1.material.opacity = 0.5;
        linePart2.material.opacity = 0.5;
        linePart3.material.opacity = 0.5;
        linePart4.material.opacity = 0.5;

        var obj = new THREE.Object3D();
        obj.add(linePart1);
        obj.add(linePart2);
        obj.add(linePart3);
        obj.add(linePart4);

        obj.rotation.z=angle;

        whole.add(obj);


        obj.position.set((getWidth+getWidth/4)*Math.cos(angle)*y+getX,getY+(getWidth+getWidth/4)*Math.sin(angle)*y,-20);

        var a=Math.PI*2;
        a=a/(count);
        obj.rotateX(a*y);


        arrPosition.push(obj.position.x);
        arrPosition.push(obj.position.y);
        arrPosition.push(obj.position.z);
        obj.position.set(obj.position.x+Math.random()*400-200,obj.position.y+Math.random()*400-200,obj.position.z+Math.random()*400-200);

        // linePart1.material.opacity=0;
        // linePart2.material.opacity=0;
        // linePart3.material.opacity=0;
        // linePart4.material.opacity=0;

            for (var w = 0; w < obj.children.length; w++) {
                obj.children[w].material.opacity = 0;
            }
    }



    whole.DoIt=function(){
        if(bl_DoIt) {
            for (var z = 0; z < whole.children.length; z++) {
                var four = whole.children[z];
                four.rotateX(0.05);
                four.position.x += valueX;
                four.position.y += valueY;
                for (var k = 0; k < four.children.length; k++) {
                    var c = four.children[k];
                    var percent;
                    if (XYDirection == "X") {
                        percent = (getX - four.position.x) / minusX;
                    }
                    else {
                        percent = (getY - four.position.y) / minusY;
                    }

                    c.material.color.r = greenColor.r * (percent);
                    c.material.color.g = greenColor.g * (percent);
                    c.material.color.b = greenColor.b * (percent);
                    c.material.opacity = 1 - percent;
                }
                if (XYDirection == "X") {
                    if (iX == 1) {
                        if (four.position.x > ToX) {
                            four.position.x = getX;
                            four.position.y = getY;
                            for (var k = 0; k < four.children.length; k++) {
                                var c = four.children[k];
                                c.material.color.r = greenColor.r;
                                c.material.color.g = greenColor.g;
                                // c.material.color.b = greenColor.b;
                            }
                        }
                    }
                    else {
                        if (four.position.x < ToX) {
                            four.position.x = getX;
                            four.position.y = getY;
                            for (var k = 0; k < four.children.length; k++) {
                                var c = four.children[k];
                                c.material.color.r = greenColor.r;
                                c.material.color.g = greenColor.g;
                                c.material.color.b = greenColor.b;
                            }
                        }
                    }
                }
                else {
                    if (iY == 1) {
                        if (four.position.y > ToY) {
                            four.position.x = getX;
                            four.position.y = getY;
                            for (var k = 0; k < four.children.length; k++) {
                                var c = four.children[k];
                                c.material.color.r = greenColor.r;
                                c.material.color.g = greenColor.g;
                                // c.material.color.b = greenColor.b;
                            }
                        }
                    }
                    else {
                        if (four.position.y < ToY) {
                            four.position.x = getX;
                            four.position.y = getY;
                            for (var k = 0; k < four.children.length; k++) {
                                var c = four.children[k];
                                c.material.color.r = greenColor.r;
                                c.material.color.g = greenColor.g;
                                c.material.color.b = greenColor.b;
                            }
                        }
                    }
                }
            }
        }
    }



    //线段飘散
    // var arrPosition=[];
    whole.split=function(bl){
            if(bl){
                bl_DoIt=false;
                arrPosition=[];
                for(var z=0;z<whole.children.length;z++){
                    var a=whole.children[z];
                    arrPosition.push(whole.children[z].position.x);
                    arrPosition.push(whole.children[z].position.y);
                    arrPosition.push(whole.children[z].position.z);
                    TweenMax.to(a.position,1+Math.random(),{x:Math.random*1000-500,y:Math.random*1000-500,z:Math.random()*1000-500,ease:Cubic.easeOut});
                    for(var y=0;y<a.children.length;y++){
                        var b=a.children[y];
                        TweenMax.to(b.material,1.3,{delay:0.01*z,opacity:0,ease:Cubic.easeOut});
                    }
                }
            }
            else{
                for(var z=0;z<whole.children.length;z++){
                    var a=whole.children[z];
                    if(z==0) {
                    }
                    TweenMax.to(a.position,1,{x:arrPosition[z*3],y:arrPosition[z*3+1],z:arrPosition[z*3+2],ease:Cubic.easeOut});
                    for(var y=0;y<a.children.length;y++){
                        var b=a.children[y];
                        TweenMax.to(b.material,0.5,{delay:0.0*z,opacity:0.4,ease:Cubic.easeOut});
                    }
                }
                bl_DoIt = true;


            }
        }

    whole.dispose=function(){

    }
    return whole;


}